﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace SGArchiveSystem7x.Controllers.SP
{
    public class SPController : BaseController
    {
        private string sControllerName = "SP";
        // GET: SP
        public ActionResult Index()
        {
            var n = this.VaildUser(sControllerName);
            if (n != null)
                return n;
            var columns = SGArchiveSystem7x.Models.CColumnsModel.Create_SPFlowColumns();
            ViewBag.Columns = columns;
            ViewBag.DEPID = App_Code.CSession.Get_User(Session).U_DEPID;
            return View();
        }
        /// <summary>
        /// 获取审批信息
        /// </summary>
        /// <param name="depid"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Get_ajaxSPFlowArchive(int depid)
        {
            int count;
            var list = SGArchiveSystem7xDB.Linq.T_SPFlowDal.Get_SPFlow(out count,depid, 0, App_Code.CWebCommons._iPageRows);
            List<string> list1 = new List<string>(1);
            list1.Add(depid.ToString());
            App_Code.CSession.Add_QuerySp(Session, list1);
            var json = new
            {
                rows = list.ToArray(),
                total = count
            };
            return Json(json, JsonRequestBehavior.AllowGet);
        }
        
        [HttpPost]
        public ActionResult Get_ajaxSPFlowArchivePager(int num, int size)
        {
            ///分页后的起始行
            int iStart = (num - 1) * size;
            var listWhere = SGArchiveSystem7x.App_Code.CSession.Get_QuerySp(Session);
            int count = 0;
            var list = SGArchiveSystem7xDB.Linq.T_SPFlowDal.Get_SPFlow(out count, int.Parse(listWhere[0]), iStart, App_Code.CWebCommons._iPageRows); ;
            var json = new
            {
                rows = list.ToArray(),
                total = count
            };
            return Json(json, JsonRequestBehavior.AllowGet); 
        }
        public int FlowSP(string result, string[] id,string suggest)
        {
            SGArchiveSystem7xDB.T_SPFlow en = new SGArchiveSystem7xDB.T_SPFlow();
            for (int i = 0; i < id.Length; i++)
            {
                var user = App_Code.CSession.Get_User(Session);
                en.SPF_ID = int.Parse(id[i]);
                en.SPF_DATE = DateTime.Now;
                en.SPF_UID = user.U_ID;
                en.SPF_UNAME = user.U_RNAME;
                en.SPF_Result = int.Parse(result);
                en.SPF_Status = 1;//已审批
                en.SPF_Suggest = suggest;
                SGArchiveSystem7xDB.Linq.T_SPFlowDal.SP_SPFlow(en);
            }
            return 1;
        }
        public ActionResult SPPZ()
        {
            var QUERY = SGArchiveSystem7xDB.Linq.T_SPFlowDal.Get_SPContent().SP_STATUS.Split(',');
            ViewBag.status1 = QUERY[0];
            ViewBag.status2 = QUERY[1];
            ViewBag.status3 = QUERY[2];
            return View();
        }
        public string SaveSPPZ(string status)
        {
            SGArchiveSystem7xDB.Linq.T_SPFlowDal.Modify_SPContent(status);
            return "1";
        }
    }
}